package com.yellowpage.ndc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import java.sql.Timestamp;
import com.yellowpage.ndc.data.ProdGroupHis;
import com.yellowpage.ndc.service.ProdGroupHisService;

public class ProdGroupHisAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<ProdGroupHis> prodGroupHisList = new ArrayList<ProdGroupHis>();
	private List<Map> resultList = new ArrayList<Map>();

	private ProdGroupHis prodGroupHis;
	private ProdGroupHisService prodGroupHisService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_groupId;
	private String query_createUser;
	private String query_createUserCode;
	private String query_createTime;
	private String query_note;
	private String query_id;
	private String query_type;
	private String query_content;

	private String query_name1;
	private String query_loginName;
	private String query_createTime_end;
	private String query_prodgroupId;
	private Map hisMap = new HashMap();

	public void prepare() {
		super.prepare();
		hisMap.put("0", "新增");
		hisMap.put("1", "修改");
		hisMap.put("2", "删除");

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.prodGroupHis = prodGroupHisService.getProdGroupHis(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("groupId", query_groupId);
		queryMap.put("prodgroupId", query_prodgroupId);
		queryMap.put("name1", query_name1);
		queryMap.put("loginName", query_loginName);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createUserCode", query_createUserCode);
		queryMap.put("createTime", query_createTime);
		queryMap.put("createTime_end", query_createTime_end);
		queryMap.put("note", query_note);
		queryMap.put("id", query_id);
		queryMap.put("type", query_type);
		queryMap.put("content", query_content);

		resultList = prodGroupHisService.queryProdGroupHisByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		prodGroupHis.setCreateUser(Integer.valueOf(loginUser.getUserId()
				.toString()));
		prodGroupHis.setCreateTime(new Timestamp(System.currentTimeMillis()));
		prodGroupHisService.saveProdGroupHis(prodGroupHis);
		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (prodGroupHis.getNote().length() == 0) {
			this.addActionError("** 备注 不能为空 !");
		}
		if (prodGroupHis.getContent().length() == 0) {
			this.addActionError("** 内容 不能为空 !");
		}

		this.setValidatorResult("/ndc/ProdGroupHisAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		prodGroupHis = prodGroupHisService.getProdGroupHis(id);

		return RESULT_EDIT;
	}

	public String update() {

		prodGroupHisService.updateProdGroupHis(prodGroupHis);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (prodGroupHis.getNote().length() == 0) {
			this.addActionError("** 备注 不能为空 !");
		}
		if (prodGroupHis.getContent().length() == 0) {
			this.addActionError("** 内容 不能为空 !");
		}
		this.setValidatorResult("/ndc/ProdGroupHisUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.prodGroupHisService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突，删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setProdGroupHis(ProdGroupHis prodGroupHis) {
		this.prodGroupHis = prodGroupHis;
	}

	public ProdGroupHis getProdGroupHis() {
		return prodGroupHis;
	}

	public void setProdGroupHisService(ProdGroupHisService prodGroupHisService) {
		this.prodGroupHisService = prodGroupHisService;
	}

	public List<ProdGroupHis> getProdGroupHisList() {
		return prodGroupHisList;
	}

	public void setProdGroupHisList(List<ProdGroupHis> prodGroupHisList) {
		this.prodGroupHisList = prodGroupHisList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_groupId() {
		return this.query_groupId;
	}

	public void setQuery_groupId(String groupId) {
		this.query_groupId = groupId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_id() {
		return this.query_id;
	}

	public void setQuery_id(String id) {
		this.query_id = id;
	}

	public String getQuery_type() {
		return this.query_type;
	}

	public void setQuery_type(String type) {
		this.query_type = type;
	}

	public String getQuery_content() {
		return this.query_content;
	}

	public void setQuery_content(String content) {
		this.query_content = content;
	}

	public Map getHisMap() {
		return hisMap;
	}

	public void setHisMap(Map hisMap) {
		this.hisMap = hisMap;
	}

	public ProdGroupHisService getProdGroupHisService() {
		return prodGroupHisService;
	}

	public String getQuery_createUserCode() {
		return query_createUserCode;
	}

	public void setQuery_createUserCode(String query_createUserCode) {
		this.query_createUserCode = query_createUserCode;
	}

	public String getQuery_name1() {
		return query_name1;
	}

	public void setQuery_name1(String query_name1) {
		this.query_name1 = query_name1;
	}

	public String getQuery_loginName() {
		return query_loginName;
	}

	public void setQuery_loginName(String query_loginName) {
		this.query_loginName = query_loginName;
	}

	public String getQuery_createTime_end() {
		return query_createTime_end;
	}

	public void setQuery_createTime_end(String query_createTime_end) {
		this.query_createTime_end = query_createTime_end;
	}

	public String getQuery_prodgroupId() {
		return query_prodgroupId;
	}

	public void setQuery_prodgroupId(String query_prodgroupId) {
		this.query_prodgroupId = query_prodgroupId;
	}

}
